import { request } from '@/framework/requests'
import { buildGetApi } from '@/framework/apis'
import { CommonStringObjectType, LabelValue } from '@/framework/types/common-data'
import { Ref } from 'vue'

export const getDictList = (dictName: string) =>
  request(buildGetApi('/dict'), { dictName }, {}, false, false) as Promise<any>
export const getDictNameList = (params: object) =>
  request(buildGetApi('/item/admin/list/dictTitle'), params, {}, false, false) as Promise<any>

export const getTreeList = (dictName: string) =>
  request(buildGetApi('/tree'), { dictName }, {}, false, false) as Promise<any>

export const getDictListByDictName = (dictName: string, targetVar: Ref) =>
  getDictList(dictName).then(({ payload }) => {
    targetVar.value = payload
    const dictMap: CommonStringObjectType = {}
    payload.forEach(({ label, value }: LabelValue) => (dictMap[label] = value))
    return dictMap
  })

export const getTreeListByDictName = (dictName: string, targetVar: Ref) =>
  getTreeList(dictName).then(({ payload }) => {
    targetVar.value = payload
  })

export const getAllTreeDict = () => request(buildGetApi('/tree/admin/all'), {}, {}, true) as Promise<any>
